package com.wechat.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import com.swak.exception.BaseRuntimeException;
import com.wechat.service.StorageService;
import com.wechat.service.storage.StorageServiceImpl;

/**
 * 存储配置
 * 
 * @author 618lf
 */
@Configuration
@EnableConfigurationProperties(StorageProperties.class)
public class StorageConfig {

	@Autowired
	private ResourceLoader resourceLoader;

	@Bean
	public StorageService storageService(StorageProperties properties) {
		return new StorageServiceImpl(loadStoragePath(properties));
	}

	/**
	 * 加载存储的路径
	 * 
	 * @return
	 */
	private String loadStoragePath(StorageProperties properties) {
		String location = properties.getPath();
		try {
			Resource resource = resourceLoader.getResource(location);
			return resource.getFile().getAbsolutePath();
		} catch (Exception e) {
			throw new BaseRuntimeException(e);
		}
	}
}
